import java.util.HashMap;
import java.util.LinkedList;


public class Car extends Expr {
	private Expr e;
	
	public Car(Expr e) {
		this.e = e;
	}

    public Element eval(HashMap<String, Element> nametable, HashMap<String, Proc> functiontable, LinkedList var) {
    	Element list = e.eval(nametable, functiontable, var);
    	
    	if (!list.isList()) {
    		throw new IllegalStateException("Expression does not evaluate to a list.");
    	}
    	
    	int numElements = list.getListValue().getSequence().numElements();
    	
    	if (numElements == 0) {
    		throw new IllegalStateException("List contains no elements. Cannot do car.");
    	}
    	
        return list.getListValue().getSequence().getElements().get(0).clone();
    }
}
